package com.woniu.adopt_web.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.adopt_web.mapper.TeamMapper;
import com.woniu.adopt_web.vo.TeamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class TeamService {

    @Autowired
    TeamMapper teamMapper;


    public PageInfo<TeamVo> queryTeamByPage(Integer now, Integer size, String name) {

        List<TeamVo> teamVos = teamMapper.selectTeamByName(name);
        List<TeamVo> temp = new ArrayList<>();
        teamVos.forEach(teamVo -> {
            List<Integer> list = new ArrayList();
            teamVo.getRoleList().forEach(r -> {
                        if ("超级管理员".equals(r.getName())) {
                            list.add(0);
                        } else if ("普通管理员".equals(r.getName())) {
                            list.add(1);
                        } else if ("救助站工作人员".equals(r.getName())) {
                            list.add(2);
                        } else if ("志愿者".equals(r.getName())) {
                            list.add(3);
                        } else if ("特邀作家".equals(r.getName())) {
                            list.add(4);
                        }
                    }
            );
            //list为空，说明非团队成员从团队中排除掉,在迭代器直接移除会报出来了一个异常：ConcurrentModificationException，原因是使用迭代器迭代过程中改变了迭代集合中的元素
            //解决办法：待移除数据先暂存到temp，然后在最后移除
            if (list.size()==0) {
                temp.add(teamVo);
            } else {
                int min = Collections.min(list);
                switch (min) {
                    case 0: {
                        teamVo.setTypeName("队长");
                        teamVo.setType(0);
                        break;
                    }
                    case 1: {
                        teamVo.setTypeName("组员");
                        teamVo.setType(1);
                        break;
                    }
                    case 2: {
                        teamVo.setTypeName("救护站工作人员");
                        teamVo.setType(2);
                        break;
                    }
                    case 3: {
                        teamVo.setTypeName("志愿者");
                        teamVo.setType(3);
                        break;
                    }
                    case 4: {
                        teamVo.setTypeName("特邀作家");
                        teamVo.setType(4);
                        break;
                    }
                    default: {
                        break;
                    }
                }

            }
        });
        /*这个写法[idea提示]同
        temp.forEach(t->{
         teamVos.remove(t);})
         */
        temp.forEach(teamVos::remove);
        System.out.println(teamVos);
        //按照队长-组员-救护站工作人员-志愿者-特邀作家的顺序派排序
        teamVos.sort(new Comparator<TeamVo>() {
            @Override
            public int compare(TeamVo o1, TeamVo o2) {
                Integer o1Type = o1.getType();
                Integer o2Type = o2.getType();
                return o1Type.compareTo(o2Type);
            }
        });
        //PageHelper需要挪到这里！不然憨批乱分页！！！！
        PageHelper.startPage(now, size);
        return new PageInfo<>(teamVos);
    }

}
